package com.dodo.giveback;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mylhyl.superdialog.SuperDialog;

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE = 111;

    Button[] btns = new Button[9];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /*ZXingLibrary.initDisplayOpinion(this);
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);*/
        init();
        random();

        showDialog("提示","请按1-9顺序点击按钮进行屏幕检测","我知道了",null);
    }

    public void showDialog(String title,String content,String btnName,SuperDialog.OnClickPositiveListener listener){
        new SuperDialog.Builder(this).setRadius(20)
                .setAlpha(1).setCancelable(false)
                .setTitle(title).setMessage(content)
                .setPadding(200,100,200,100)
                .setPositiveButton(btnName==null ?"确定":btnName, listener).build();
    }


    public void init(){
        btns[0] = (Button)findViewById(R.id.btn1);
        btns[1] = (Button)findViewById(R.id.btn2);
        btns[2] = (Button)findViewById(R.id.btn3);
        btns[3] = (Button)findViewById(R.id.btn4);
        btns[4] = (Button)findViewById(R.id.btn5);
        btns[5] = (Button)findViewById(R.id.btn6);
        btns[6] = (Button)findViewById(R.id.btn7);
        btns[7] = (Button)findViewById(R.id.btn8);
        btns[8] = (Button)findViewById(R.id.btn9);
    }

    int flag = 1;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void check(View view){
        Button b = (Button)view;
        String value = (String) b.getText();
        Log.e("eeeeeeeeeeeeeeeeeeee",flag +"---------------------------"+value);
        if (!"".equals(value) && Integer.parseInt(value) != flag){
            Toast.makeText(this,"输入错误,请重新输入",Toast.LENGTH_LONG).show();
            random();
            flag = 1;
        }else{
            if (flag == 9){
                Intent intent = new Intent(this,QcCodeActivity.class);
                startActivity(intent);
                this.finish();
            }

            flag ++;
            //定义底部标签图片大小
            Drawable drawable = getResources().getDrawable(R.drawable.select);
            b.setBackground(drawable);
            b.setText("");


        }

    }


    public  void random(){
        int[] nums = randomCommon(1, 10, 9);
        for(int i = 0; i < 9; i ++){
            btns[i].setText(nums[i]+"");
            btns[i].setBackgroundColor(getResources().getColor(R.color.btn));
        }

    }



    public int[] randomCommon(int min, int max, int n){
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while(count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if(num == result[j]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                result[count] = num;
                count++;
            }
        }
        return result;
    }



}
